import get from '@dfeidao/atom-web/local/get';
import nodejs from '@dfeidao/atom-web/msg/nodejs';
import s_get from '@dfeidao/atom-web/storage/get';
import { IFeidaoAiBrowserComponent } from '@dfeidao/web/interfaces';

////////////////////////////////////////////////////////////////////////////////
// 表体

import remove from '@dfeidao/atom-web/local/remove';
import render from '@dfeidao/atom-web/render/render';
import P01 from './P01';
import P04 from './P04';
import P05 from './P05';
////////////////////////////////////////////////////////////////////////////////

export default async function a015(fd: IFeidaoAiBrowserComponent) {
	const page_no = get(fd, 'page_no') as number;
	const page_size = get(fd, 'page_size') as number;
	const productid = await s_get('productid');
	const query = {
		productid,
		page_no,
		page_size
	};
	const res = await nodejs<{ dataArray: object[], page_size: number, page_no: number }>('functional-permissions/zj-000001/s001', query);
	if (res.dataArray.length === 0) {
		render(fd, '', P01, 'P01', 'inner');
		fd.data.node.querySelector('[data-feidao-presentation="P05"]').innerHTML = '';
		render(fd, '', P04, 'P04', 'inner');
	} else {
		const tb = res.dataArray;
		const data = tb.map((v) => {
			return { ...v, v: JSON.stringify(v) };
		});
		if (res.dataArray.length >= page_size) {
			render(fd, res, P05, 'P05', 'inner');
		} else {
			fd.data.node.querySelector('[data-feidao-presentation="P05"]').innerHTML = '';
		}
		render(fd, data, P01, 'P01', 'inner');
		fd.data.node.querySelector('[data-feidao-presentation="P04"]').innerHTML = '';
	}
	res.page_size = page_size;
	if (!page_no) {
		res.page_no = 1;
	} else {
		res.page_no = page_no;
	}
	remove(fd, '_on');
	remove(fd, '_id');
	remove(fd, 'name');
}
